package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.service.ICourseService;
import cn.itsource.ymcc.domain.Course;
import cn.itsource.ymcc.query.CourseQuery;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.result.PageList;
import cn.itsource.ymcc.util.UserContext;
import cn.itsource.ymcc.vo.CourseVo;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    @PostMapping("/info")
    public JsonResult info(@RequestBody List<Long> ids){
        return courseService.info(ids);
    }

    @GetMapping("/detail/{courseId}")
    public JsonResult getDetail(@PathVariable("courseId")Long courseId){
        return courseService.getDetail(courseId);
    }

    @PostMapping("/onLineCourse")
    public JsonResult onLineCourse(@RequestBody List<Long> ids){
        return courseService.onLineCourse(ids);
    }

    @PostMapping("/add")
    public JsonResult save(@RequestBody CourseVo courseVo){
        if(courseVo.getCourse().getId() == null){
            return courseService.add(courseVo);
        }else {
            return courseService.edit(courseVo);
        }
    }

    @GetMapping("/findCourse/{id}")
    public JsonResult findCourse(@PathVariable("id")Long id){
        return courseService.findCourse(id);
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody Course course){
        if(course.getId()!=null){
            courseService.updateById(course);
        }else{
            courseService.save(course);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        courseService.removeById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success().setData(courseService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.success().setData(courseService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPageNo(), query.getPageSize());
        QueryWrapper<Course> name = new QueryWrapper<Course>()
                .like(StringUtils.hasText(query.getKeyword()), "name", query.getKeyword())
                .orderByDesc("id");
        page = courseService.page(page,name);
        return JsonResult.success().setData(new PageList<Course>(page.getTotal(), page.getRecords()));
    }
}
